package com.smartalarmclock.UI.Games;

import java.util.Random;

import com.smartalarmclock.UI.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TextInputActivity extends Activity {
	
	private TextView m_TextInputText;
	private Button m_ButtonStopAlarm;
	private EditText m_Result;
	private int m_MsgToDisplay;
	private MediaPlayer m_MediaPlayer = new MediaPlayer();

	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game_input_text);
		
		Random random = new Random();
		m_MsgToDisplay = random.nextInt(9-0);
		
		
		// A lot of copy code... will be fixed in later versions:
		try {
			Uri alert =  RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
			m_MediaPlayer.setDataSource(this, alert);
			final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			if (audioManager.getStreamVolume(AudioManager.STREAM_RING) != 0)
			{
				m_MediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
				m_MediaPlayer.setLooping(true);
				m_MediaPlayer.prepare();
				m_MediaPlayer.start();
				
			}
			
		} catch(Exception e) {
			
		}   
					
					
		addListenerOnButton();
	}

	@Override
	public void onBackPressed() { }
	
	private void addListenerOnButton() {
		m_TextInputText = (TextView) findViewById(R.id.textViewMsgToCopy);
		m_ButtonStopAlarm = (Button) findViewById(R.id.buttonStopAlarmOnInputText);
		m_Result = (EditText) findViewById(R.id.editTextResOnInputText);
		
		m_TextInputText.setText(setMsg(m_MsgToDisplay));
		
		m_ButtonStopAlarm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {			
				String res = m_Result.getText().toString();
				String originalTxt = m_TextInputText.getText().toString();
				
				if(res.equals(originalTxt))
				{
					Intent resultIntent = new Intent();
					setResult(Activity.RESULT_OK, resultIntent);
					String msgToUser = "Success!\n Have a nice day";
					Toast.makeText(TextInputActivity.this, msgToUser, Toast.LENGTH_LONG).show();
					m_MediaPlayer.stop();
					finish();
				}
				
				else
				{
					String msgToUser = "Wrong!\n Please try again";
					Toast.makeText(TextInputActivity.this, msgToUser, Toast.LENGTH_LONG).show();
				}
					
			  }
			});
	}

	private String setMsg(int msgToDisplay) {
		
		String msg;
		
		switch(msgToDisplay){
		case 1:
			msg = "PLeAse tYpe thiS mEsSage";
			break;
		case 2:
			msg = "12345654321qwertyasdfgh";
			break;
		case 3:
			msg = "I hope you slept well!";
			break;
		case 4:
			msg = "Aloha! Good morning!";
			break;
		case 5:
			msg = "1+2+3=6";
			break;
		case 6:
			msg = "I bet you can't type this message!";
			break;
		case 7:
			msg = "Homer Simpson";
			break;
		case 8:
			msg = "Puppy dog!";
			break;
		case 9:
			msg = "Everyone loves SAC!";
			break;
		default: 
			msg = "Everyone loves SAC!";
		}
		
		return msg;
	}
			
			

}
